// Copyright 2019 the Dart project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.

import 'dart:math';

import 'package:bilibili/fade_in_demo2.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/util/logger.dart';

const owlUrl =
    'https://raw.githubusercontent.com/flutter/website/main/src/content/assets/images/docs/owl.jpg';

class FadeInDemo extends StatefulWidget {
  const FadeInDemo({super.key});

  @override
  State<FadeInDemo> createState() => _FadeInDemoState();
}

class _FadeInDemoState extends State<FadeInDemo>
    with SingleTickerProviderStateMixin {
  double opacityLevel = 1.0;

  late Animation<double> sizeAnimation;
  late Animation<double> opacityAnimation;
  late AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller =
        AnimationController(duration: const Duration(seconds: 2), vsync: this);
    opacityAnimation =
        Tween<double>(begin: 0, end: 2*pi).animate(controller)
    ..addListener((){
      logger.i( "rotate:: ${opacityAnimation.value}");
    })
    ..addStatusListener((status){
      if(status == AnimationStatus.completed){
        controller.repeat();
      }else if(status == AnimationStatus.dismissed){
        controller.forward();
      }
    })
    ;

    sizeAnimation = Tween<double>(begin: 0, end: 300).animate(controller)
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse();
        } else if (status == AnimationStatus.dismissed) {
          controller.forward();
        }
        logger.i(status);
      })
      ..addStatusListener((status) {
        logger.i(status);
      });

    controller.forward();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => GrowTransition(
      sizeAnimation: sizeAnimation,
      opacityAnimation: opacityAnimation,
      child: const FlutterLogo());
}

class AnimatedLogo extends AnimatedWidget {
  const AnimatedLogo({super.key, required Animation<double> animation})
      : super(listenable: animation);

  @override
  Widget build(BuildContext context) {
    var animation = listenable as Animation<double>;

    return Center(
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 10),
        height: animation.value,
        width: animation.value,
        child: const FlutterLogo(),
      ),
    );
  }
}

class GrowTransition extends StatelessWidget {
  final Widget child;
  final Animation<double> sizeAnimation;
  final Animation<double> opacityAnimation;

  const GrowTransition(
      {required this.child,
      required this.sizeAnimation,
      required this.opacityAnimation,
      super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child:  AnimatedBuilder(
          animation: sizeAnimation,
          child: child,
          builder: (BuildContext context, Widget? child) {

            // return  SizedBox(
            //   width: sizeAnimation.value,
            //   height: sizeAnimation.value,
            //   child: child,
            // );

            return Transform.rotate(
              angle: opacityAnimation.value,
              child: SizedBox(
                width: sizeAnimation.value,
                height: sizeAnimation.value,
                child: child,
              ),
            );
          }),
    );
  }
}

class FadeInApp extends StatelessWidget {
  const FadeInApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: FadeInDemo2(),
        ),
      ),
    );
  }
}
